var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
var app = getApp();
Page({

  data: {
    // 隐藏遮罩
    hiddenMask: true,
    order_id: "",
    // rawData: "",
    good:{},
    isBusiness:false,
  },


  onLoad: function (options) {
    
    this.data.order_id = options.oid;
    this.data.type = options.type;
    // this.getOrderInfo();
  },
  onShow:function(){
    this.getOrderInfo();
  },

  getOrderInfo: function () {
    var that = this;
    var params = new Object()
    params.oid = this.data.order_id;
    params.type = this.data.type;
    util.showLoading("数据加载中..");
    api.POST({
      params: params,
      url: app.globalData.base_url + 'couponsorder/info',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          util.hiddenLoading();
          that.setData({
            rawData: res.data.data,
          });


        }
        console.log("订单详情", res);

      },
      fail: function (fail) {
        util.showErrorPath("../../../", "获取失败，请检查网络");

      }
    })


  },

  //订单信息
  orderInfo: function (oid, typeNum) {
    var that = this;
    api.POST({
      params: { oid: oid, type: typeNum },
      url: app.globalData.base_url + 'couponsorder/info',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          wx.setStorageSync('orderData', res.data.data)
        }
      }
    })

  },


  setModalStatus: function (e) {
    this.setData({
      hiddenMask: true,
    });
  },
  // 阻止冒泡
  preventClick: function () {

  },
  //开具发票
  invoice:function(){
    wx.showModal({
      title: '提示',
      showCancel:false,
      confirmText:'好的',
      confirmColor:'#e51927',
      content: '请联系店家为您开具发票',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //删除订单
  delOrder: function (e) {
    var that = this;
    var oid = that.data.order_id;
    wx.showModal({
      title: '',
      content: '是否删除该订单',
      success: function (res) {
        if (res.confirm) {
          // console.log('用户点击确定')
          api.POST({
            params: { oid: oid },
            url: app.globalData.orderDel_url,
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                // that.resetData();
                util.showStatus("删除成功");
                setTimeout(function(){
                  wx.navigateBack({
                    delta: 1
                  })
                },1500)
              } else {
                util.showError(res.data.code);
              }
            },
            fail: function (fail) {
              wx.hideLoading()
              util.showError("请检查网络");
            }
          })
        }
      }
    })
  },
  receiveOrder:function(){
    var that = this;
    var oid = that.data.order_id;
    wx.showModal({
      title: '提示',
      content: '是否确认收货',
      success: function (res) {
        if (res.confirm) {
          // console.log('用户点击确定')
          api.POST({
            params: { oid: oid },
            url: app.globalData.base_url + 'order/receipt',
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                // that.resetData();
                util.showStatus("收货成功");
                 that.getOrderInfo();
              } else {
                util.showError(res.data.code);
              }
            },
            fail: function (fail) {
              wx.hideLoading()
              util.showError("请检查网络");
            }
          })
        }
      }
    })
  },
  //支付接口
  pay: function (e) {
    var that = this;
    var oid = that.data.order_id;
    var typeNum = this.data.type;
    that.orderInfo(oid, typeNum);
    if (typeNum == 0) {
      var urlText = 'getCouponsWechatInfo'
    } else {
      var urlText = 'getWechatInfo'
    }
    api.POST({
      params: { oid: oid },
       url: app.globalData.base_url + 'payment/' + urlText,
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          var preData = res.data.data.payinfo;
          wx.requestPayment({
            timeStamp: preData.timeStamp,
            nonceStr: preData.nonceStr,
            package: preData.package,
            signType: preData.signType,
            paySign: preData.paySign,
            success: function (res) {
              if (typeNum == 0) {
                wx.switchTab({
                  url: '../index/index'
                })
              } else {
                wx.redirectTo({
                  url: '../paySuccess/paySuccess?oid=' + oid + "&&types=" + typeNum
                })
              }
            },
            fail: function (fail) {
              wx.removeStorageSync('orderData');
              util.showError("支付失败");
            }
          })
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })
  },
  contactCall:function(){
    wx.makePhoneCall({
      phoneNumber: '4000-788-519' 
    })
  }

})